package com.cskaoyan.market.controller.Admin;

import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.service.admin.MarketStorageService;
import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/admin/storage/")
//解析文件
@MultipartConfig
public class AdminStorageController extends HttpServlet {
    @Autowired
    private MarketStorageService marketStorageService;


    @PostMapping("delete")
    public Object delete(@RequestBody Map<String, String> map) throws IOException {

        String id = map.get("id");
        MarketStorage marketStorage = new MarketStorage();
        assert id != null;
        marketStorage.setId(Integer.parseInt(id));
        marketStorageService.delete(marketStorage);
        return ResponseUtil.ok(marketStorage);
    }

    @PostMapping("update")
    public Object update(@RequestBody Map<String,String> map) throws IOException {

        String id = map.get( "id");
        String name = map.get( "name");
        MarketStorage marketStorage = new MarketStorage();
        assert id != null;
        marketStorage.setId(Integer.parseInt(id));
        marketStorage.setName(name);
        marketStorage.setUpdateTime(LocalDateTime.now());
        marketStorageService.update(marketStorage);
        return ResponseUtil.ok(marketStorage);

    }

    @PostMapping("create")
    public Object create(HttpServletRequest req, HttpServletResponse resp, MultipartFile file) throws ServletException, IOException {
        long size = file.getSize();
        String contentType = file.getContentType();
        String fileName = file.getOriginalFilename();
        String key = UUID.randomUUID() + "-" + fileName;

//        String basePath = "D:\\software_development\\javaproj\\image";
        //TODO 修改路径
        file.transferTo(new File("D:/software_development/javaproj/image/" + key));

        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setDeleted(false);
        marketStorage.setAddTime(LocalDateTime.now());
        marketStorage.setUpdateTime(LocalDateTime.now());
        String relativePath = "/wx/storage/fetch/" + key;
        marketStorage.setUrl(relativePath);
        marketStorage.setType(contentType);
        //key是最终的名称；name是上传的名字
        marketStorage.setKey(key);
        marketStorage.setName(fileName);
        marketStorage.setSize((int) size);
        //随后将要保存到数据库
        marketStorageService.create(marketStorage);
        //在这个地方再对url进行处理，追加上主机端口号
        //此时，经过数据库存储之后id便有了值，然后对url进行处理
        String url = marketStorage.getUrl();
        marketStorage.setUrl("http://localhost:8083" + url);
        //响应出去
        return ResponseUtil.ok(marketStorage);
    }

    @GetMapping("list")
    public Object list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String name = req.getParameter("name");
        String key = req.getParameter("key");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        Integer page = null;
        Integer limit = null;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
        } catch (NumberFormatException e) {
            return ResponseUtil.badArgument();
            //throw new RuntimeException(e);
        }
        List<MarketStorage> marketStorageList = marketStorageService.list(limit, page, name, key, sort, order);

        for (MarketStorage marketStorage : marketStorageList) {
            marketStorage.setUrl("http://localhost:8083" + marketStorage.getUrl());
        }
        return ResponseUtil.okList(marketStorageList);

    }
}
